﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StreetlightGames.XnaEngine;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using StreetlightGames.XnaEngine.Particles;
using StreetlightGames.SpaceShooter.Enemies;

namespace StreetlightGames.SpaceShooter
{
    public class PlayerShip : ModelObject
    {
        private const float SPEED = 500.0f;

        /// <summary>Gets the SpaceShooterGame value</summary>
        public SpaceShooterGame SpaceShooterGame
        {
            get { return Game as SpaceShooterGame; }
        }

        /// <summary>Gets or sets the MinX value</summary>
        public float MinX
        {
            get { return _minX; }
            set { _minX = value; }
        }
        private float _minX = SpaceShooterGame.MinX;

        /// <summary>Gets or sets the MaxX value</summary>
        public float MaxX
        {
            get { return _maxX; }
            set { _maxX = value; }
        }
        private float _maxX = SpaceShooterGame.MaxX;

        /// <summary>Gets the Missiles value</summary>
        public List<Missile> Missiles
        {
            get { return _missiles; }
        }
        private List<Missile> _missiles = new List<Missile>();

        /// <summary>Gets the Player value</summary>
        public Player Player
        {
            get { return _player; }
        }
        private Player _player;

        public PlayerShip(Game game, Player player)
            : base(game)
        {
            ModelName = "Models\\GalagaShip";
            Scale = 5.0f;

            _player = player;
        }

        public override void Initialize()
        {
            for (int i = 0; i < 20; i++)
            {
                Missile missile = new Missile(Game);
                missile.Camera = Camera;
                Game.Components.Add(missile);
                _missiles.Add(missile);
            }
            base.Initialize();
        }

        internal void MoveLeft()
        {
            if (Position.X >= MinX)
            {
                Velocity = new Vector3(-SPEED, 0.0f, 0.0f);
            }
            else
            {
                Position = new Vector3(MinX, 0.0f, 0.0f);
            }
        }

        internal void MoveRight()
        {
            if (Position.X <= MaxX)
            {
                Velocity = new Vector3(SPEED, 0.0f, 0.0f);
            }
            else
            {
                Position = new Vector3(MaxX, 0.0f, 0.0f);
            }
        }

        internal void Stop()
        {
            Velocity = Vector3.Zero;
        }

        internal void FireMissile()
        {
            foreach (Missile missile in _missiles)
            {
                if (!missile.IsAlive)
                {
                    missile.Position = Position;
                    missile.Launch();
                    break;
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (Player.State == PlayerState.Playing)
            {
                CheckMissiles(SpaceShooterGame.Wave);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        internal virtual void Explode()
        {
            IsAlive = false;
            Player.Lives--;
            AudioController.Instance.PlayCue("GalagaExplode");
            for (int i = 0; i < 100; i++)
            {
                SpaceShooterGame.ExplosionSystem.AddParticle(Position, Velocity);
            }
            if (!Player.IsDoubledUp)
            {
                Player.State = PlayerState.Exploding;
            }
        }

        internal void Start()
        {
            foreach (Missile missile in Missiles)
            {
                missile.IsAlive = false;
            }
        }

        public void CheckMissiles(Wave wave)
        {
            foreach (Missile missile in Missiles)
            {
                if (missile.IsAlive)
                {
                    foreach (EnemyShip ship in wave.Ships)
                    {
                        if (ship.IsAlive)
                        {
                            if (VectorHelper.GetDistance(ship.Position, missile.Position) < 50)
                            {
                                missile.Explode();
                                int oldScore = SpaceShooterGame.Score;
                                SpaceShooterGame.Score += ship.CalculateScore(this);
                                if (SpaceShooterGame.Score >= 10000 && oldScore < 10000)
                                {
                                    Player.AddFreeShip();
                                }
                                else if (SpaceShooterGame.Score >= 50000 && oldScore < 50000)
                                {
                                    Player.AddFreeShip();
                                }
                                MotherShip motherShip = ship as MotherShip;
                                if (motherShip != null)
                                {
                                    if (motherShip.IsShipCaptured && motherShip.IsHit)
                                    {
                                        Player.StartDoubleUp(motherShip.Position);
                                    }
                                }
                                ship.Explode();
                            }
                        }
                    }
                }
            }
        }
    }
}
